Constants

DEBUG_LINE_LENGTH

DEBUG_LINE_LENGTH = 4096

DEFAULT_QUOTE

DEFAULT_QUOTE = '`'

TYPE_SQL

TYPE_SQL = 'sql'

TYPE_INT

TYPE_INT = 'integer'

TYPE_BOOL

TYPE_BOOL = 'bool'

TYPE_STRING

TYPE_STRING = 'string'

Properties

$db_provider

$db_provider : string

Type

string — Database implementation (mysql, postgres, etc.)

$db_dsnw

$db_dsnw

$db_dsnr

$db_dsnr

$db_dsnw_array

$db_dsnw_array

$db_dsnr_array

$db_dsnr_array

$db_connected

$db_connected

$db_mode

$db_mode

$db_pconn

$db_pconn

$dbh

$dbh

$dbhs

$dbhs

$table_connections

$table_connections

$db_error

$db_error

$db_error_msg

$db_error_msg

$conn_failure

$conn_failure

$db_index

$db_index

$last_result

$last_result

$tables

$tables

$variables

$variables

$options

$options

Methods

factory()

factory(string  $db_dsnw, string  $db_dsnr = '', bool  $pconn = false) : \rcube_db

Factory, returns driver-specific instance of the class

Parameters

string $db_dsnw

DSN for read/write operations

string $db_dsnr

Optional DSN for read only operations

bool $pconn

Enables persistent connections

Returns

\rcube_db —

Object instance

__construct()

__construct(string  $db_dsnw, string  $db_dsnr = '', bool  $pconn = false) : mixed

Object constructor

Parameters

string $db_dsnw

DSN for read/write operations

string $db_dsnr

Optional DSN for read only operations

bool $pconn

Enables persistent connections

Returns

mixed —

db_connect()

db_connect(string  $mode, bool  $force = false) : mixed

Connect to appropriate database depending on the operation

Parameters

string $mode

Connection mode (r|w)

bool $force

Enforce using the given mode

Returns

mixed —

set_debug()

set_debug(bool  $dbg = true) : mixed

Activate/deactivate debug mode

Parameters

bool $dbg

True if SQL queries should be logged

Returns

mixed —

is_error()

is_error(mixed  $result = null) : string

Getter for error state

Parameters

mixed $result

Optional query result

Returns

string —

Error message

is_connected()

is_connected() : bool

Connection state checker

Returns

bool —

True if in connected state

is_replicated()

is_replicated() : bool

Is database replication configured?

Returns

bool —

Returns true if dsnw != dsnr

get_variable()

get_variable(string  $varname, mixed  $default = null) : mixed

Get database runtime variables

Parameters

string $varname

Variable name

mixed $default

Default value if variable is not set

Returns

mixed —

Variable value or default

query()

query() : \PDOStatement|false

Execute a SQL query

Returns

\PDOStatement|false —

Query handle or False on error

limitquery()

limitquery() : \PDOStatement|false

Execute a SQL query with limits

Returns

\PDOStatement|false —

Query handle or False on error

insert_or_update()

insert_or_update(string  $table, array  $keys, array  $columns, array  $values) : \PDOStatement|bool

INSERT ... ON DUPLICATE KEY UPDATE (or equivalent).

When not supported by the engine we do UPDATE and INSERT.

Parameters

string $table

Table name (should be already passed via table_name() with quoting)

array $keys

Hash array (column => value) of the unique constraint

array $columns

List of columns to update

array $values

List of values to update (number of elements should be the same as in $columns)

Returns

\PDOStatement|bool —

Query handle or False on error

affected_rows()

affected_rows(mixed  $result = null) : int

Get number of affected rows for the last query

Parameters

mixed $result

Optional query handle

Returns

int —

Number of (matching) rows

num_rows()

num_rows(mixed  $result = null) : mixed

Get number of rows for a SQL query If no query handle is specified, the last query will be taken as reference

Parameters

mixed $result

Optional query handle

Returns

mixed —

Number of rows or false on failure

insert_id()

insert_id(string  $table = '') : mixed

Get last inserted record ID

Parameters

string $table

Table name (to find the incremented sequence)

Returns

mixed —

ID or false on failure

fetch_assoc()

fetch_assoc(mixed  $result = null) : array|false

Get an associative array for one row If no query handle is specified, the last query will be taken as reference

Parameters

mixed $result

Optional query handle

Returns

array|false —

Array with col values or false on failure

fetch_array()

fetch_array(mixed  $result = null) : array|false

Get an index array for one row If no query handle is specified, the last query will be taken as reference

Parameters

mixed $result

Optional query handle

Returns

array|false —

Array with col values or false on failure

list_tables()

list_tables() : array

Returns list of tables in database

Returns

array —

List of all tables of the current database

list_cols()

list_cols(string  $table) : array

Returns list of columns in database table

Parameters

string $table

Table name

Returns

array —

List of table cols

startTransaction()

startTransaction() : bool

Start transaction

Returns

bool —

True on success, False on failure

endTransaction()

endTransaction() : bool

Commit transaction

Returns

bool —

True on success, False on failure

rollbackTransaction()

rollbackTransaction() : bool

Rollback transaction

Returns

bool —

True on success, False on failure

reset()

reset() : mixed

Release resources related to the last query result.

When we know we don't need to access the last query result we can destroy it and release memory. Useful especially if the query returned big chunk of data.

Returns

mixed —

closeConnection()

closeConnection() : mixed

Terminate database connection.

Returns

mixed —

quote()

quote(mixed  $input, string  $type = null) : string

Formats input so it can be safely used in a query

Parameters

mixed $input

Value to quote

string $type

Type of data (integer, bool, ident)

Returns

string —

Quoted/converted string for use in query

escape()

escape(string  $str) : string

Escapes a string so it can be safely used in a query

Parameters

string $str

A string to escape

Returns

string —

Escaped string for use in a query

quoteIdentifier()

quoteIdentifier(string  $str) : string

Quotes a string so it can be safely used as a table or column name

Parameters

string $str

Value to quote

Returns

string —

Quoted string for use in query

escapeSimple()

escapeSimple(string  $str) : string

Escapes a string so it can be safely used in a query

Parameters

string $str

A string to escape

Returns

string —

Escaped string for use in a query

quote_identifier()

quote_identifier(string  $str) : string

Quotes a string so it can be safely used as a table or column name

Parameters

string $str

Value to quote

Returns

string —

Quoted string for use in query

param()

param(mixed  $value, string  $type = null) : mixed

Create query parameter object

Parameters

mixed $value

Parameter value

string $type

Parameter type (one of rcube_db::TYPE_* constants)

Returns

mixed —

now()

now(int  $interval) : string

Return SQL function for current time and date

Parameters

int $interval

Optional interval (in seconds) to add/subtract

Returns

string —

SQL function to use in query

array2list()

array2list(array  $arr, string  $type = null) : string

Return list of elements for use with SQL's IN clause

Parameters

array $arr

Input array

string $type

Type of data (integer, bool, ident)

Returns

string —

Comma-separated list of quoted values for use in query

unixtimestamp()

unixtimestamp(string  $field) : string

Return SQL statement to convert a field value into a unix timestamp

Parameters

string $field

Field name

Returns

string —

SQL statement to use in query

fromunixtime()

fromunixtime(int  $timestamp) : string

Return SQL statement to convert from a unix timestamp

Parameters

int $timestamp

Unix timestamp

Returns

string —

Date string in db-specific format

ilike()

ilike(string  $column, string  $value) : string

Return SQL statement for case insensitive LIKE

Parameters

string $column

Field name

string $value

Search value

Returns

string —

SQL statement to use in query

concat()

concat() : string

Abstract SQL statement for value concatenation

Returns

string —

SQL statement to be used in query

encode()

encode(mixed  $input, bool  $serialized = false) : mixed

Encodes non-UTF-8 characters in string/array/object (recursive)

Parameters

mixed $input

Data to fix

bool $serialized

Enable serialization

Returns

mixed —

Properly UTF-8 encoded data

decode()

decode(mixed  $input, bool  $serialized = false) : mixed

Decodes encoded UTF-8 string/object/array (recursive)

Parameters

mixed $input

Input data

bool $serialized

Enable serialization

Returns

mixed —

Decoded data

table_name()

table_name(string  $table, bool  $quoted = false) : string

Return correct name for a specific database table

Parameters

string $table

Table name

bool $quoted

Quote table identifier

Returns

string —

Translated table name

set_option()

set_option(string  $name, mixed  $value) : mixed

Set class option value

Parameters

string $name

Option name

mixed $value

Option value

Returns

mixed —

set_table_dsn()

set_table_dsn(string  $table, string  $mode) : mixed

Set DSN connection to be used for the given table

Parameters

string $table

Table name

string $mode

DSN connection ('r' or 'w') to be used

Returns

mixed —

parse_dsn()

parse_dsn(mixed  $dsn) : array

MDB2 DSN string parser

Parameters

mixed $dsn

Returns

array —

DSN parameters

exec_script()

exec_script(string  $sql) : bool

Execute the given SQL script

Parameters

string $sql

SQL queries to execute

Returns

bool —

True on success, False on error

dsn_connect()

dsn_connect(array  $dsn, string  $mode) : mixed

Connect to specific database

Parameters

array $dsn

DSN for DB connections

string $mode

Connection mode (r|w)

Returns

mixed —

conn_create()

conn_create(mixed  $dsn) : mixed

Create PDO connection

Parameters

mixed $dsn

Returns

mixed —

conn_prepare()

conn_prepare(mixed  $dsn) : mixed

Prepare connection

Parameters

mixed $dsn

DSN for DB connections

Returns

mixed —

conn_configure()

conn_configure(mixed  $dsn, mixed  $dbh) : mixed

Configure connection, create database if not exists

Parameters

mixed $dsn

DSN for DB connections

mixed $dbh

Connection handler

Returns

mixed —

dsn_select()

dsn_select(mixed  $query) : mixed

Analyze the given SQL statement and select the appropriate connection to use

Parameters

mixed $query

Returns

mixed —

debug()

debug(string  $query) : mixed

Writes debug information/query to 'sql' log file

Parameters

string $query

SQL query

Returns

mixed —

_query()

_query(string  $query, int  $offset, int  $numrows, array  $params) : \PDOStatement|false

Execute a SQL query with limits

Parameters

string $query

SQL query to execute

int $offset

Offset for LIMIT statement

int $numrows

Number of rows for LIMIT statement

array $params

Values to be inserted in query

Returns

\PDOStatement|false —

Query handle or False on error

query_execute()

query_execute(mixed  $query) : mixed

Query execution

Parameters

mixed $query

Returns

mixed —

query_parse()

query_parse(string  $query) : string

Parse SQL query and replace identifier quoting

Parameters

string $query

SQL query

Returns

string —

SQL query

handle_error()

handle_error(string  $query) : mixed

Helper method to handle DB errors.

This by default logs the error but could be overridden by a driver implementation

Parameters

string $query

Query that triggered the error

Returns

mixed —

Result to be stored and returned

_fetch_row()

_fetch_row(mixed  $result, int  $mode) : array|false

Get col values for a result row

Parameters

mixed $result

Optional query handle

int $mode

Fetch mode identifier

Returns

array|false —

Array with col values or false on failure

set_limit()

set_limit(string  $query, int  $limit, int  $offset) : string

Adds LIMIT,OFFSET clauses to the query

Parameters

string $query

SQL query

int $limit

Number of rows

int $offset

Offset

Returns

string —

SQL query

dsn_string()

dsn_string(mixed  $dsn) : string

Build DSN string for PDO constructor

Parameters

mixed $dsn

DSN parameters

Returns

string —

DSN string

dsn_options()

dsn_options(array  $dsn) : array

Returns driver-specific connection options

Parameters

array $dsn

DSN parameters

Returns

array —

Connection options

fix_table_names()

fix_table_names(mixed  $sql) : mixed

Parse SQL file and fix table names according to table prefix

Parameters

mixed $sql

Returns

mixed —

fix_table_names_callback()

fix_table_names_callback(mixed  $matches) : mixed

Preg_replace callback for fix_table_names()

Parameters

mixed $matches

Returns

mixed —